﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GPRSLib;
using GPRSForm.Database;
using System.IO.Ports;
namespace GPRSForm.Send_Interface
{
    public partial class NormalSend : UserControl
    {
        gmsClass gms = null;
        SerialPort port = null;
        public NormalSend()
        {
            InitializeComponent();
            this.Name = "normalsend";
            port = GPRSForm.GlobalClass.portVar;
            gms = GPRSForm.GlobalClass.gmsVar;
        }
        
        private void send_Click(object sender, EventArgs e)
        {

            string phoneNum = null;
            string text = null;
            try
            {
                if (phoneText.Text == "" || smsText.Text == "")
                {
                    MessageBox.Show("Input not blank", "Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    phoneNum = this.phoneText.Text;
                    text = this.smsText.Text;
                    sendSMS(phoneNum, text);
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /*
        * method send SMS
        */
        public bool sendSMS(string phoneNumber, string smg)
        {

            // send message with phone and message
            if (gms.sendMsg(port, phoneNumber, smg))
            {
                //this.label_result.Text = "Message is sent successfully";
                return true;
            }
            else
            {
                //this.label_result.Text = "Fail to sent message";
                return false;
            }
        }

        private void NormalSend_EnabledChanged(object sender, EventArgs e)
        {
            if (this.Enabled)
            {
                port = GPRSForm.GlobalClass.portVar;
                gms = GPRSForm.GlobalClass.gmsVar;
            }
        }

        private void switchAutoSend_Click(object sender, EventArgs e)
        {
            changeToAutoMode();
        }
        private void changeToAutoMode()
        {
            Control[] list = this.Parent.Controls.Find("autosend", true);
            if (list != null)
            {
                Control autosend = list[0];
                this.Visible = false;
                this.Enabled = false;
                autosend.Enabled = true;
                autosend.Visible = true;
            }
        }

        
    }
}
